{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7f7c3284",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/llm/vercel-ai-gateway.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a07f0af-6f8b-4a4e-a984-5f0f161dd0c3",
   "metadata": {},
   "source": [
    "# Vercel AI Gateway"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc0d66a7",
   "metadata": {},
   "source": [
    "The AI Gateway is a proxy service from Vercel that routes model requests to various AI providers. It offers a unified API to multiple providers and gives you the ability to set budgets, monitor usage, load-balance requests, and manage fallbacks. You can find out more from their [docs](https://vercel.com/docs/ai-gateway)\n",
    "\n",
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ddc8c84",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-llms-vercel-ai-gateway"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "425f649c",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02bfb427-2607-4322-bdaa-f012a87a0112",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.vercel_ai_gateway import VercelAIGateway\n",
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "llm = VercelAIGateway(\n",
    "    model=\"anthropic/claude-4-sonnet\",\n",
    "    max_tokens=64000,\n",
    "    context_window=200000,\n",
    "    api_key=\"your-api-key\",\n",
    "    default_headers={\n",
    "        \"http-referer\": \"https://myapp.com/\",  # Optional: Your app URL\n",
    "        \"x-title\": \"My App\",  # Optional: Your app name\n",
    "    },\n",
    ")\n",
    "\n",
    "print(llm.model)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3907f07b-a33a-46db-b799-fec83843ff60",
   "metadata": {},
   "source": [
    "## Call `chat` with ChatMessage List\n",
    "You need to either set env var `VERCEL_AI_GATEWAY_API_KEY` or `VERCEL_OIDC_TOKEN` or set api_key in the class constructor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0bec6d7-d5cc-4c23-aa95-a915e65220cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import os\n",
    "# os.environ['VERCEL_AI_GATEWAY_API_KEY'] = '<your-api-key>'\n",
    "\n",
    "llm = VercelAIGateway(\n",
    "    api_key=\"pBiuCWfswZCDxt8D50DSoBfU\",\n",
    "    max_tokens=64000,\n",
    "    context_window=200000,\n",
    "    model=\"anthropic/claude-4-sonnet\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1135fe2a-b4ff-4352-a825-bdc3aca17b60",
   "metadata": {},
   "outputs": [],
   "source": [
    "message = ChatMessage(role=\"user\", content=\"Tell me a joke\")\n",
    "resp = llm.chat([message])\n",
    "print(resp)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0515c6b2-e691-4f89-baa2-4964abee9cc5",
   "metadata": {},
   "source": [
    "### Streaming"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "099f51ad-d585-4bf4-b0e1-95ed7ecf3f85",
   "metadata": {},
   "outputs": [],
   "source": [
    "message = ChatMessage(role=\"user\", content=\"Tell me a story in 250 words\")\n",
    "resp = llm.stream_chat([message])\n",
    "for r in resp:\n",
    "    print(r.delta, end=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac92c80f-5b82-4143-a6f9-549d6f5dfa70",
   "metadata": {},
   "source": [
    "## Call `complete` with Prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1290c9b1-3c37-4db0-aa94-dda1c90d4f8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = llm.complete(\"Tell me a joke\")\n",
    "print(resp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be1a7fe6-51b7-4e80-b60d-fbe3335610c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = llm.stream_complete(\"Tell me a story in 250 words\")\n",
    "for r in resp:\n",
    "    print(r.delta, end=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc3a2018-89f1-4795-9b68-c06b8f104a69",
   "metadata": {},
   "source": [
    "## Model Configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88c2680d-5e21-4eba-a685-a30d64554b14",
   "metadata": {},
   "outputs": [],
   "source": [
    "# This example uses Anthropic's Claude 4 Sonnet (models are specified as `provider/model`):\n",
    "llm = VercelAIGateway(\n",
    "    model=\"anthropic/claude-4-sonnet\",\n",
    "    api_key=\"pBiuCWfswZCDxt8D50DSoBfU\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24ebd16b-1740-4f31-800d-9c9c8fcc0d96",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = llm.complete(\"Write a story about a dragon who can code in Rust\")\n",
    "print(resp)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
